/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.test.base;

import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.account.core.repository.service.AccountRepository;
import info.peigen.hotpot.business.customer.core.repository.entity.PartnerEntity;
import info.peigen.hotpot.business.customer.core.repository.mapper.*;
import info.peigen.hotpot.business.customer.core.repository.service.*;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import org.beetl.sql.solon.annotation.Db;
import org.noear.solon.annotation.Inject;

/**
 * <b>(CustomerRepositoryTestBase)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/4/25
 */
@FieldDefaults(level = AccessLevel.PROTECTED)
public class CustomerRepositoryTestBase extends CustomerTestBase {
    @Inject
    CustomerRepository                   customerRepository;
    @Inject
    CustomerOperatorRepository           customerOperatorRepository;
    @Inject
    CustomerAuthenticationRepository     customerAuthenticationRepository;
    @Inject
    CustomerAuthenticationBaseRepository customerAuthenticationBaseRepository;
    @Inject
    PartnerRepository                    partnerRepository;
    @Inject
    AccountRepository                    accountRepository;
    @Db
    CustomerMapper                       customerMapper;
    @Db
    CustomerAuthenticationMapper         customerAuthenticationMapper;
    @Db
    CustomerAuthenticationBaseMapper     customerAuthenticationBaseMapper;
    @Db
    CustomerAuthenticationLogMapper      customerAuthenticationLogMapper;
    @Db
    CustomerTechMapper                   customerTechMapper;
    @Db
    CustomerOperatorMapper               customerOperatorMapper;

    protected void cleanAccountAndLog(String customerId) {
        accountRepository.getMapper().deleteByCustomerId(customerId);
        accountRepository.getMapper().deleteByAccountId(customerId);
        accountRepository.getAccountLogRepository().getMapper().deleteByAccountId(customerId);
        accountRepository.getAccountTransLogRepository().getMapper().deleteByAccountId(customerId);
        accountRepository.getAccountFreezeLogRepository().getMapper().deleteByAccountId(customerId);
    }

    protected void cleanPartnerByName(String partnerName) {
        PartnerEntity entity = partnerRepository.getMapper().findByPartnerName(partnerName);
        if (ObjectUtil.isNull(entity)) {
            return;
        }
        cleanPartnerByPartnerId(entity.getPartnerId());
    }


    protected void cleanPartnerByPartnerId(String partnerId) {
        partnerRepository.getMapper().deleteByPartnerId(partnerId);
        cleanAccountAndLog(partnerId);
    }

}
